package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;

import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        //把参数map集合转换成Hospital对象
        String str = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(str, Hospital.class);

        //判断是否存在该数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);

        //如果不存在，添加数据
        //如果存在，则修改数据
        if(hospitalExist != null){
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else{
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //设置查询条件
        Hospital hospital = new Hospital();
        if(hospitalQueryVo != null){
            BeanUtils.copyProperties(hospitalQueryVo,hospital);
        }
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Hospital> example = Example.of(hospital, exampleMatcher);
        //设置Page
        PageRequest pageRequest = PageRequest.of(page-1,limit);
        Page<Hospital> hospitalPage = hospitalRepository.findAll(example, pageRequest);

        //Hospital中并没有医院等级属性，在此需要远程调用数据字典中的信息，补充每个hospital对象
        //获取Page中的对象，并返回其List集合
        List<Hospital> content = hospitalPage.getContent();
        //遍历集合，进行医院等级的封装
        content.stream().forEach(item -> {
            this.setHospitalHosType(item);
        });
        return hospitalPage;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        //根据id获取hospital对象
        Hospital hospital = hospitalRepository.findById(id).get();
        //设置修改的值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String,Object> getHospById(String id) {
        HashMap<String, Object> map = new HashMap<>();
        Hospital hospital = setHospitalHosType(hospitalRepository.findById(id).get());
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return map;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //获取医院详情
        Hospital hospital = this.getByHoscode(hoscode);
        result.put("hospital",hospital);
        //预约规则
        result.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return result;
    }


    //将遍历出的item补充医院等级
    private Hospital setHospitalHosType(Hospital item) {
        String hostypeString = dictFeignClient.getName("Hostype", item.getHostype());
        //查询省市区
        String provinceCode = dictFeignClient.getName(item.getProvinceCode());
        String cityCode = dictFeignClient.getName(item.getCityCode());
        String districtCode = dictFeignClient.getName(item.getDistrictCode());
        item.getParam().put("hostypeString",hostypeString);
        item.getParam().put("fullAddress",provinceCode+cityCode+districtCode);
        return item;
    }
}
